//
//  NickNameChangedViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/23.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "NickNameChangedViewController.h"

@interface NickNameChangedViewController ()

@property (strong, nonatomic) UIView *nickNameView;
@property (strong, nonatomic) UILabel *nickNameL;
//@property (strong, nonatomic) UILabel *nickName;
@property (strong, nonatomic) UITextField *nickNnameTextField;
@property (strong, nonatomic) UIView *line;

@property (strong, nonatomic) UILabel *tipL;

@property (strong, nonatomic) UIImageView *saveIV;
@property (strong, nonatomic) UILabel *saveL;

@end

@implementation NickNameChangedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = WhiteColor;
    self.title = @"修改昵称";
    
    [self createUI];
    
}

#pragma mark -保存
- (void)saveClick:(UIButton *)btn {
    
    NSLog(@"昵称修改保存");
    
    if (self.nickNnameTextField.text.length <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入昵称"];
        return ;
    }
    
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"user_name\" : \"%@\"}", [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey], [RSA encryptString:self.nickNnameTextField.text publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"user/User/Updateusername" parameters:@{@"json" : par} success:^(id responseObject) {
        
        NSLog(@"修改昵称：%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
//            [[KYAlertView sharedInstance] showAlertViewWithMessage:@"昵称修改成功"];
             [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            [CommonUserInfo sharedInstance].nickName = self.nickNnameTextField.text;
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
        
    }];
    
}



- (void)createUI {
    
    [self.view addSubview:self.nickNameView];
    [self.nickNameView addSubview:self.nickNameL];
    [self.nickNameView addSubview:self.nickNnameTextField];
    [self.nickNameView addSubview:self.line];
    
    [self.view addSubview:self.tipL];
    [self.view addSubview:self.saveIV];
    [self.view addSubview:self.saveL];
    
    __weak typeof(self) weakSelf = self;
    [self.nickNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.height.mas_equalTo(jisuan(100));
    }];
    [self.nickNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.nickNameView.mas_left).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.nickNameView.mas_centerY);
        make.width.mas_equalTo(jisuan(50));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.nickNnameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.nickNameL.mas_right).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.nickNameView.mas_centerY);
        make.right.mas_equalTo(weakSelf.nickNameView.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.nickNameView.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.nickNameView.mas_right).with.offset(-jisuan(40));
        make.bottom.mas_equalTo(weakSelf.nickNameView.mas_bottom);
        make.height.mas_equalTo(jisuan(2));
    }];
    
    [self.tipL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.top.mas_equalTo(weakSelf.nickNameView.mas_bottom).with.offset(jisuan(28));
    }];
    
    [self.saveIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(weakSelf.tipL.mas_bottom).with.offset(jisuan(80));
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(106));
    }];
    [self.saveL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.saveIV.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.saveIV.mas_centerY).with.offset(-jisuan(4));
    }];
    
}

#pragma mark -lazy
- (UIView *)nickNameView {
    
    if (!_nickNameView) {
        
        _nickNameView = [[UIView alloc] init];
        _nickNameView.backgroundColor = WhiteColor;
    
    }
    
    return _nickNameView;
}

- (UILabel *)nickNameL {
    
    if (!_nickNameL) {
        
        _nickNameL = [[UILabel alloc] init];
        _nickNameL.textAlignment = NSTextAlignmentLeft;
        _nickNameL.font = Font(24);
        _nickNameL.textColor = Color333333;
        _nickNameL.text = @"昵称";
        
    }
    
    return _nickNameL;
}

- (UITextField *)nickNnameTextField {
  
    if (!_nickNnameTextField) {
        
        _nickNnameTextField = [[UITextField alloc] init];
//        _nickNnameTextField.text = @"浪人痞夫";
        _nickNnameTextField.text = [CommonUserInfo sharedInstance].nickName;
        _nickNnameTextField.textColor = Color333333;
        _nickNnameTextField.font = Font(24);
        _nickNnameTextField.textAlignment = NSTextAlignmentLeft;
        
        _nickNnameTextField.keyboardType = UIKeyboardTypeDefault;
        
    }
    
    return _nickNnameTextField;
}

- (UIView *)line {
    
    if (!_line) {
        
        _line = [[UIView alloc] init];
        _line.backgroundColor = Colordddddd;
    
    }
    return _line;
}

- (UILabel *)tipL {
    
    if (!_tipL) {
        
        _tipL = [[UILabel alloc] init];
        _tipL.text = @"注意：昵称最多可输入12个字符哦～";
        _tipL.textColor = Color999999;
        _tipL.font = Font(20);
        _tipL.textAlignment = NSTextAlignmentLeft;
    }
    return _tipL;
}


- (UIImageView *)saveIV {
    
    if (!_saveIV) {
        
        _saveIV = [[UIImageView alloc] init];
        _saveIV.image = [UIImage imageNamed:@"redBtnBG"];
        _saveIV.contentMode = UIViewContentModeScaleAspectFit;
        
        _saveIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(saveClick:)];
        [_saveIV addGestureRecognizer:recognizer];
        
    }
    
    return _saveIV;
}

- (UILabel *)saveL {
    
    if (!_saveL) {
        
        _saveL = [[UILabel alloc] init];
        _saveL.text = @"保存";
        _saveL.textColor = WhiteColor;
        _saveL.font = Font(32);
        _saveL.textAlignment = NSTextAlignmentCenter;
    }
    return _saveL;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController./tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
